package cn.sunshine.ymcc.security;

import cn.sunshine.ymcc.domain.Permission;
import cn.sunshine.ymcc.service.IPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;

import java.util.List;
@EnableGlobalMethodSecurity(securedEnabled = true,prePostEnabled= true)
@Configuration
@EnableWebSecurity // 开启web安全配置
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private IPermissionService permissionService;

    //配置认证管理器，授权模式为“poassword”时会用到
    @Bean
    public AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }
    //提供用户信息，这里没有从数据库查询用户信息，在内存中模拟
  /*  @Bean
    public UserDetailsService userDetailsService(){
        InMemoryUserDetailsManager inMemoryUserDetailsManager = 
        new InMemoryUserDetailsManager();  // 这是在 内存中写死的登录人
        inMemoryUserDetailsManager.createUser(User.withUsername("zs").password("123").authorities("admin").build());
        return inMemoryUserDetailsManager;
    }*/
  
   /* public static void main(String[] args) {
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        // 加密

        String pwd1 = encoder.encode("123");
        String pwd2 = encoder.encode("123");
        System.out.println(pwd1);
        System.out.println(pwd2); */
    // 解密对比
       /* boolean ma1 = encoder.matches("123", "$2a$10$O.NywmpMemn/f3QTDtpnsuRE/ke55/FbgP0hUmzXS.yV2WKXejzt2");
        boolean ma2 = encoder.matches("123", "$2a$10$M0B.OwSXFqV/Ml5qr6w1uOMKN188GLC9QGsfOybKAKp5ZiY7yV88S");
        System.out.println(ma1);
        System.out.println(ma2);
    }*/


   /* //密码编码器：不加密
    @Bean
    public PasswordEncoder passwordEncoder(){
        return NoOpPasswordEncoder.getInstance(); // 明文密码比较器
    }*/
    // 容器的加密算法
   @Bean
   public PasswordEncoder passwordEncoder(){
       return new BCryptPasswordEncoder(); // 密码比较器
   }
    //授权规则配置
    @Override
    protected void configure(HttpSecurity http) throws Exception {
       /* // web授权
        List<Permission> permissions = permissionService.selectList(null);
        permissions.stream().forEach(p->{
            try {
                http.authorizeRequests().antMatchers(p.getResource()).hasAuthority(p.getSn());
            } catch (Exception e) {
                e.printStackTrace();
            }
        });*/

        http.authorizeRequests()                                //授权配置
                .antMatchers("/login","/logout").permitAll()  //登录路径放行
                .anyRequest().authenticated()                   //其他路径都要认证之后才能访问
                .and().formLogin()                              //允许表单登录
               // .successForwardUrl("/login/loginSuccess")             // 设置登陆成功页
                .and().logout().permitAll()                    //登出路径放行 /logout
                .and().csrf().disable();                        //关闭跨域伪造检查
    }
}